<?php
class ControllerModuleProMegaMenu extends Controller {
	private $error = array(); 
	
	public function index() {
		$this->load->model('module/pro_megamenu');
		$this->model_module_pro_megamenu->index();
		$this->data['menu_data'] = $this->model_module_pro_megamenu->getlist();
		$this->load->language('module/pro_megamenu');

		$this->document->setTitle($this->language->get('meta_title'));
		
		$this->load->model('setting/setting');
				
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			$this->save();		
					
			$this->session->data['success'] = $this->language->get('text_success');
						
			$this->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'));
		}
				
		$this->data['heading_title'] = $this->language->get('meta_title');
		
		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');
		$this->data['button_remove'] = $this->language->get('button_remove');
		$this->data['button_add_menu'] = $this->language->get('button_add_menu');
		$this->data['button_add_option'] = $this->language->get('button_add_option');
		$this->data['button_remove_option'] = $this->language->get('button_remove_option');
				
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

  		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_module'),
			'href'      => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('module/pro_megamenu', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
		$this->data['action'] = $this->url->link('module/pro_megamenu/index', 'token=' . $this->session->data['token'], 'SSL');
		
		$this->data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL');
		
		$this->data['token'] = $this->session->data['token'];
		
		$this->load->model('catalog/category');
		$this->data['categories'] = $this->model_catalog_category->getCategories(0);
		
		$this->load->model('catalog/manufacturer');
		$results = $this->model_catalog_manufacturer->getManufacturers();

        foreach ($results as $result) {
			$this->data['manufacturers'][] = array(
                'id' => $result['manufacturer_id'],
                'name'       	  => $result['name']
            );
        }
		
		$this->load->model('catalog/information');
		$results_info = $this->model_catalog_information->getInformations();
 
    	foreach ($results_info as $result) {
			$this->data['informations'][] = array(
				'id' => $result['information_id'],
				'title'          => $result['title']
			);
		}
		$this->document->addScript('view/javascript/pro_megamenu/jquery.nestable.js');
		$this->document->addScript('view/javascript/pro_megamenu/pro_megamenu.js');
		$this->document->addStyle('view/stylesheet/pro_megamenu/pro_megamenu.css');
		$this->template = 'module/pro_megamenu.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}
	protected function save() {
		$this->load->model('module/pro_megamenu');
		$this->model_module_pro_megamenu->empty_data();		
		if(isset($this->request->post['title'])){
			$data['title'] = $this->request->post['title'];
			$data['type'] = $this->request->post['type'];
			$data['parent_id'] = $this->request->post['parent_id'];
			$data['type_id'] = $this->request->post['type_id'];
			$data['url'] = $this->request->post['url'];
			$data['columns'] = $this->request->post['columns'];
			$data['widths'] = $this->request->post['widths'];
			$data['content_submenu'] = $this->request->post['content_submenu'];
			$data['activemega'] = $this->request->post['activemega'];
			$this->model_module_pro_megamenu->insert($data);
		}		

	}
	private function validate() {
		if (!$this->user->hasPermission('modify', 'module/pro_megamenu')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
		
		if (!$this->error) {
			return true;
		} else {
			return false;
		}	
	}
}
?>